package com.example.mylearn.executors.ThreadPool.blockQueue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class ArrayBlockQueueExample {






    public static void main(String[] args) throws Exception {
        BlockingQueue queue = new ArrayBlockingQueue(4);

        Producer producer = new Producer(queue);
        Consumer consumer = new Consumer(queue);

        new Thread(producer).start();
//        new Thread(consumer).start();
        Thread.sleep(4000);
    }


    static class Producer implements Runnable {
        private final BlockingQueue queue;
        public Producer(BlockingQueue queue) {
            this.queue = queue;
        }
        public void run() {
            try {
                for (int i = 0; i < 5; i++) {
                    System.out.println("Producer: " + i);
                    queue.put(i);
                    Thread.sleep(3000);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    static class Consumer implements Runnable {
        private final BlockingQueue queue;
        public Consumer(BlockingQueue queue) {
            this.queue = queue;
        }
        public void run() {
            try {
                for (int i = 0; i < 5; i++) {
                    System.out.println("Consumer: " + queue.take());
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }




}
